package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/protobuf/types/known/emptypb"
	"mxshop_srvs/goods_srv/proto"
)

var bannerClient proto.GoodsClient
var conn *grpc.ClientConn

func main() {
	Init()
	//TestCreateBanner()
	//TestBannerList()
	//TestUpdateBanner()
	TestDeleteBanner()

	//conn.Close()
}

func TestUpdateBanner() {
	banner, err := bannerClient.UpdateBanner(context.Background(), &proto.BannerRequest{
		Id:  2,
		Url: "http://www.baidu.com",
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(banner)
}

/*
*
创建banner
*/
func TestCreateBanner() {
	banner, err := bannerClient.CreateBanner(context.Background(), &proto.BannerRequest{
		Index: 4,
		Image: "2.jpg",
		Url:   "http://sina.baidu.com",
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(banner)
}

func TestBannerList() {
	list, err := bannerClient.BannerList(context.Background(), &emptypb.Empty{})
	if err != nil {
		panic(err)
	}
	fmt.Println(list.Total)
	for _, banner := range list.Data {
		fmt.Println(banner)
	}
}

func TestDeleteBanner() {
	banner, err := bannerClient.DeleteBanner(context.Background(), &proto.BannerRequest{
		Id: 6,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(banner)
}

// 初始化
func Init() {
	var err error
	conn, err := grpc.Dial("192.168.31.78:50052", grpc.WithInsecure())
	if err != nil {
		panic(err)
	}
	bannerClient = proto.NewGoodsClient(conn)
}
